package utils;

import net.minecraft.server.v1_15_R1.NBTTagCompound;
import net.minecraft.server.v1_15_R1.NBTTagString;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import utils.COLOR;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UTIL {
    public static void EXECUTE(String cmd) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
    }

    public static void EXECUTE(Player player, String cmd) {
        player.chat(cmd);
    }

    public static void EXECUTE(CommandSender sender, String cmd) {
        EXECUTE(cmd);
    }

    public static void NOTICE(String msg) {
        Bukkit.getServer().broadcastMessage(msg);
    }

    public static void SEND(Player player, String msg) {
        player.sendMessage(msg);
    }

    public static void SEND(Player player, String[] msg) {
        player.sendMessage(msg);
    }

    public static void SEND(CommandSender sender, String msg) {
        sender.sendMessage(msg);
    }

    public static void SEND(CommandSender sender, String[] msg) {
        sender.sendMessage(msg);
    }

    public static void SEND(String msg) {
        SEND(Bukkit.getConsoleSender(), msg);
    }

    public static void SEND(String[] msg) {
        SEND(Bukkit.getConsoleSender(), msg);
    }

    public static boolean CHECK(String param, String[] list) {
        for (String temp : list) if (param.equalsIgnoreCase(temp)) return true;
        return false;
    }

    public static void LOG(COLOR color, String msg) {
        SEND(color.getValue() + "[RedenPlugin]" + COLOR.WHITE.getValue() + msg);
    }

    public static ItemStack ITEM(Map map) {
        net.minecraft.server.v1_15_R1.ItemStack nms = CraftItemStack.asNMSCopy(new ItemStack((Material) map.get("MATERIAL")));
        NBTTagCompound cpd = (nms.hasTag()) ? nms.getTag() : new NBTTagCompound();
        if (map.containsKey("CLICK"))
            cpd.set("Click", NBTTagString.a((String) map.get("CLICK")));
        if (map.containsKey("LEFT"))
            cpd.set("Left", NBTTagString.a((String) map.get("LEFT")));
        if (map.containsKey("RIGHT"))
            cpd.set("Right", NBTTagString.a((String) map.get("RIGHT")));
        nms.setTag(cpd);
        ItemStack is = CraftItemStack.asBukkitCopy(nms);
        ItemMeta im = is.getItemMeta();
        if (map.containsKey("NAME"))
            im.setDisplayName((String) map.get("NAME"));
        if (map.containsKey("LORE"))
            im.setLore((List<String>) map.get("LORE"));
        is.setItemMeta(im);
        return is;
    }

    public static Inventory MENU(String title, Integer size, HashMap<Integer, HashMap> map) {
        Inventory inv = Bukkit.createInventory(null, size * 9, title);
        ItemStack blank = ITEM(new HashMap() {{
            put("NAME", " ");
            put("MATERIAL", Material.GLASS_PANE);
        }});

        for (int i = 0; i < size * 9; i++)
            inv.setItem(i, map.containsKey(i) ? ITEM(map.get(i)) : blank);
        return inv;
    }
}
